<template>
  <div style="height: 100%">
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="searchForm.name" clearable @clear="findByPage" placeholder="文件名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="findByName" size="medium" icon="el-icon-search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="uploadFile" v-if="sys_file_upload" size="medium" icon="el-icon-upload">上传文件</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-tooltip content="刷新" placement="top">
            <el-button icon="el-icon-refresh-right" circle @click="findByPage"></el-button>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <!--      <vue-table :columns="columns" :data="tableData" @findPage="findByPage" @selectionChange="selectionChange"></vue-table>-->
      <v-table :columns="columns" :data="tableData" @handleClick="handleClick" @findPage="findByPage"
               :loading="loadingData"
               @selectionChange="selectionChange" :table-operation="tableOperation">
        <template slot="operation" slot-scope="scope">
          <el-button
            @click="preview(scope.data.row)"
            :size="'mini'"
            icon="el-icon-view"
            type="text">
            预览
          </el-button>
          <el-button
            v-if="sys_file_download"
            @click="handleDownload(scope.data.row)"
            :size="'mini'"
            icon="el-icon-download"
            type="text">
            下载
          </el-button>
          <el-button
            v-if="sys_file_delete"
            @click="handleDelete(scope.data.row)"
            :size="deleteOperation.size || 'mini'"
            :style="{color: deleteOperation.color|| ''}"
            :icon="deleteOperation.icon"
            type="text">
            {{deleteOperation.name}}
          </el-button>
        </template>
      </v-table>
      <el-dialog
        title="文件上传"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="cancel">
        <el-upload
          ref="upload"
          action=""
          :http-request="upload"
          :auto-upload="true"
          :on-success="handleSuccess"
          :on-change="handleChange"
          :file-list="fileList"
        >
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        </el-upload>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
<!--          <el-button type="primary" @click="uploadFile">上 传</el-button>-->
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import vueTable from '../../../components/vueTable.vue'
  import VTable from "../../../components/table/table";
  import {fileApi} from "../../../http/modules/file";
  import website from "../../../const/website";
  import {mapGetters} from "vuex";
  import {roleApi} from "../../../http/modules/role";

  export default {
    name: "log",
    components: {
      VTable,
      vueTable
    },
    data() {
      return {
        tableData: {
          totalSize: 10,
          content: []
        },
        pageRequest: {
          pageNum: 1,
          pageSize: 10
        },
        columns: [
          {prop: "fileNewName", label: "文件名", minWidth: 100},
          {prop: "fileOriginalName", label: "原文件名", minWidth: 100},
          {prop: "fileType", label: "文件类型", minWidth: 100},
          {prop: "fileSize", label: "文件大小(KB)", minWidth: 100},
          {prop: "username", label: "上传人", minWidth: 100},
          {prop: "createTime", label: "创建时间", minWidth: 100}
        ],
        tableOperation: {
          operation: true
        },
        deleteOperation: website.deleteOperation,
        searchForm: {
          name: '',
          pageNum: 0,
          pageSize: 10
        },
        selections: [],
        dialogVisible: false,
        fileList: [],
        loadingData: true
      }
    },
    methods: {
      findByPage(data) {
        if (data != null) {
          this.pageRequest = data
        }
        if (this.searchForm.name !== '' && data != null) {
          this.searchForm.pageNum = data.pageNum
          this.findByName()
        } else {
          fileApi.findPage(this.pageRequest).then(data => {
            this.tableData = data.data
            this.loadingData = false
          })
        }

      },
      findByName() {
        fileApi.findByName(this.searchForm).then(res => {
          this.tableData = res.data
        }).catch(error => {
          console.log(error)
          this.$message.error('网络错误，查询失败')
        })
      },
      preview(row) {
        console.log(row.fileNewName.substring(row.fileNewName.indexOf('.') + 1))
        let suffix = row.fileNewName.substring(row.fileNewName.indexOf('.') + 1)

        if (suffix === 'doc' || suffix === 'ppt' || suffix === 'docx' || suffix === 'xls' || suffix === 'pptx' || suffix === 'xlsx') {
          // office文件预览
          let href = 'http://view.officeapps.live.com/op/view.aspx?src=http://computerheqw.top.:8080/' + row.relativePath
          this.display(row, href)
        } else {
          // 图片、pdf预览
          let href = 'http://182.92.235.211:8080/' + row.relativePath
          this.display(row, href)
        }
      },
      // 文件预览
      display (row, href) {
        let a = document.createElement('a')
        let e = document.createEvent('MouseEvents')
        // 创建鼠标事件对象初始化事件对象
        e.initEvent('click', false, false)
        // 设置下载地址
        a.href = href
        a.target = '_blank'
        // 给指定的元素，执行事件click事件
        a.dispatchEvent(e)
      },
      handleDownload(row) {
       let relativePath = row.relativePath
        window.location.href = "http://localhost:8081/sys/file/downloadFile?relativePath=" + relativePath
      },
      handleClick(row, type) {
        let rows = []
        if (type === 'delete') {
          rows.push(row)
          this.handleDelete(rows)
        }
      },
      handleDelete(row) {
        let rows = []
        rows.push(row)
        // 删除用户
        this.$confirm('此操作将删除文件，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delete(rows)
        }).catch(() => {
          this.$message.info('已取消删除')
        })

      },
      delete(datas) {
        let records = []
        datas.forEach(item => {
          records.push({fileId: item.fileId})
        })
        fileApi.delete(records).then(res => {
          this.$message.success('删除成功')
          this.findByPage()
        }).catch(error => {
          this.$message.error('删除失败')
        })
      },
      handleBatchDelete() {
        this.handleDelete(this.selections)
      },
      selectionChange(selections) {
        this.selections = selections
      },
      uploadFile() {
        this.dialogVisible = true
        // this.upload()
        console.log(this.fileList)
      },
      upload(item) {
      let files = []
        files.push(item.file)
        let param = {userId: this.$store.getters.userInfo.userId}
        fileApi.upload(param, files).then(res => {
          this.$message.success('上传成功')
          this.dialogVisible = false
          this.findByPage()
        }).console.log(error => {
          console.log(error)
          this.$message.success('上传失败')
        })

      },
      cancel() {
        this.dialogVisible = false
      },
      handleSuccess() {
        this.$message.success('上传成功')
      },
      handleChange (file, fileList) {
        this.fileList = []
        this.fileList.push(file)
      }
    },
    computed: {
      ...mapGetters(["permissions"])
    },
    created() {
      this.sys_file_upload = this.permissions["sys:file:upload"]
      this.sys_file_download = this.permissions["sys:file:download"]
      this.sys_file_delete = this.permissions["sys:file:delete"]
    },
    mounted() {
      // this.findByPage(this.pageRequest)
    }
  }
</script>

<style scoped>

</style>
